//
//  LMJRadioPlayController.m
//  EPA
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "LMJRadioPlayController.h"
#import "LMJRedioListModel.h"
#import "LMJAVManger.h"
@interface LMJRadioPlayController ()
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
//封面
@property (weak, nonatomic) IBOutlet UIImageView *coverImageView;
//当前时间
@property (weak, nonatomic) IBOutlet UILabel *currentLable;
//总时间
@property (weak, nonatomic) IBOutlet UILabel *totalLable;
//播放进度
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
//定时器alllist
@property (nonatomic, strong) NSTimer *playTimer;

@property (weak, nonatomic) IBOutlet UIButton *playButton;
@end

@implementation LMJRadioPlayController


- (void)viewDidLoad {
    [super viewDidLoad];
    //导航栏的透明度
//    self.navigationController.navigationBar.alpha = NO;
    //标题 .封面
    LMJRedioListModel *model = self.radioArray[self.currentIndex];
    self.titleLable.text =model.title;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    [LMJAVManger sharManger].currentIndex = self.currentIndex;
    //数组初始化
    NSMutableArray *itemArray = [NSMutableArray new];
    
    //调用图片旋转
    [self startDiskAnimation:self.coverImageView];
    
    //解析
    for (LMJRedioListModel *model in self.radioArray) {
        NSURL *musicUrl = [NSURL URLWithString:model.playInfoModel.musicUrl];
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:musicUrl];
        [itemArray addObject:item];
    }
    //AVPlayerItem数组
    [LMJAVManger sharManger].itemArray = itemArray;
    [[LMJAVManger sharManger] play];
    //初始化timer
    self.playTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    
}
- (void) timerAction {
    //当前时间
    float current = [LMJAVManger sharManger].player.currentTime.value / [LMJAVManger sharManger].player.currentTime.timescale;
    NSString *currentStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)current/60,(NSInteger)current%60];
    self.currentLable.text = currentStr;
    //总时间
    float total =0;
    if ([LMJAVManger sharManger].player.currentItem.duration.timescale > 0) {
        total = [LMJAVManger sharManger].player.currentItem.duration.value / [LMJAVManger sharManger].player.currentItem.duration.timescale;
    }
  
    NSString *totalStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)total/60,(NSInteger)total%60];
    self.totalLable.text = totalStr;
    //
    if (total > 0) {
        float rate = current / total;
        self.rateSlider.value = rate;
    }
}
//上一曲
- (IBAction)didClickPreviousButton:(id)sender {
    [[LMJAVManger sharManger] previous];
    //图片和标题
    self.currentIndex --;
    if (self.currentIndex < 0) {
        self.currentIndex = self.radioArray.count - 1 ;
    }
    LMJRedioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLable.text = model.title;
    //上曲切换暂停图片
    [self.playButton setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
}
//下一曲
- (IBAction)didClickNextButton:(id)sender {
    [[LMJAVManger sharManger] next];
    //图片标题
    self.currentIndex ++;
    if (self.currentIndex >= self.radioArray.count) {
        self.currentIndex = 0;
    }
    LMJRedioListModel *model = self.radioArray[self.currentIndex];
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLable.text = model.title;
    //下曲切换暂停图片
    [self.playButton setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
    //毛玻璃效果
//    UIImageView *imageView =[[UIImageView alloc]init];
//    [imageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
//    imageView.frame = self.view.bounds;
//    [self.view insertSubview:imageView aboveSubview:self.coverImageView];
//    UIBlurEffect *backbluEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
//    UIVisualEffectView *backeffectView = [[UIVisualEffectView alloc]initWithEffect:backbluEffect];
//    backeffectView.frame =imageView.bounds;
//    imageView.alpha = 0.9;
//    [imageView addSubview:backeffectView];
}
//播放暂停
- (IBAction)didClickPlayButton:(id)sender {
    if ([LMJAVManger sharManger].player.rate == 0) {
        [[LMJAVManger sharManger] play];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"播放.png"] forState:UIControlStateNormal];
    }else {
        [[LMJAVManger sharManger] pause];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"暂停.png"] forState:UIControlStateNormal];
    }
}
//改变播放进度
- (IBAction)didChangeRateAction:(UISlider *)sender {
    CMTime changeTime= CMTimeMake(sender.value *[LMJAVManger sharManger].player.currentItem.duration.value, [LMJAVManger sharManger].player.currentItem.duration.timescale);
    //调整时间
    [[LMJAVManger sharManger] seekToTime:changeTime];
}

////图片显示毛玻璃效果
//-(void)viewWillAppear:(BOOL)animated {
//   
//    //创建需要的毛玻璃特效类型
//    UIBlurEffect *bluEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
//    //毛玻璃view视图
//    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:bluEffect];
//    //添加到要有毛玻璃特效的空间中
//    effectView.frame =self.coverImageView.bounds;
//    self.coverImageView.alpha = 0.5;
//        [self.coverImageView addSubview:effectView];
//    //

//}

//图片旋转
-(void)startDiskAnimation:(UIImageView *)imageView {
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];
    CGFloat point = M_PI_4;
        animation.values = @[@(9 * point),@(8 * point),@(7 * point),@(6 * point),@(5 * point),@(4 * point),@(3 * point),@(2 * point),@(point)];
//    animation.values = @[@(point),@(point *2),@(point *3)];
    animation.repeatCount = MAXFLOAT;
    animation.duration = 15;
    self.coverImageView.layer.cornerRadius = self.coverImageView.frame.size.width /2;
    self.coverImageView.layer.masksToBounds = YES;
    [imageView.layer addAnimation:animation forKey:@""];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
